﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace Dapper.Core
{
    public class Cookies
    {
        private const string anonymousIDKey = "Dapper_AnonymousID";
        public static void Add(string name, string value)
        {
            HttpCookie cookie = new HttpCookie(name, value);
            HttpContext.Current.Response.Cookies.Add(cookie);
        }

        public static void Set(string name, string value)
        {
            HttpCookie cookie = new HttpCookie(name, value);
            HttpContext.Current.Response.Cookies.Set(cookie);
        }

        public static void Set(string name, string value, DateTime time)
        {
            HttpCookie cookie = new HttpCookie(name, value);
            HttpContext.Current.Response.Cookies.Set(cookie);
            cookie.Expires = time;
        }


        public static string Get(string name)
        {
            string val = null;
            HttpCookie cookie = HttpContext.Current.Request.Cookies[name];
            if (null != cookie)
            {
                val = cookie.Value;
            }
            return val;
        }

        public static bool Remove(string name)
        {
            try
            {
                HttpContext.Current.Request.Cookies.Remove(name);
                return true;
            }
            catch (Exception)
            {

                return false;
            }
        }

        public static string AnonymousID
        {
            get
            {
                string AID = Get(anonymousIDKey);
                if (null == AID)
                {
                    AID = Guid.NewGuid().ToString("N");
                    Set(anonymousIDKey, AID);
                }
                return AID;
            }
        }
    }
}
